package com.unionfetch.content_home.ui.dialog

import android.app.Dialog
import android.content.Context
import android.graphics.Color
import android.graphics.drawable.ColorDrawable
import android.os.Bundle
import android.view.Gravity
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.widget.ImageView
import android.widget.TextView
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.unionfetch.content_home.R
import com.unionftech.common.R.string
import com.unionfetch.content_home.adapter.SelectTradeAccountAdapter
import com.unionfetch.content_home.bean.TradeAccountModel
import com.unionftech.common.common.language.DynamicResourceManager
import com.unionftech.common.utils.LogUtils
import com.unionftech.common.utils.StatusBarUtil
import com.unionftech.common.utils.Utils


class SelectTradeAccountDialog(private val mContext: Context) : Dialog(mContext) {
    private lateinit var mLlDialogContent: View
    private lateinit var mRvAccountList: RecyclerView
    private lateinit var mTvSelectAccount: TextView
    private lateinit var mIvDialogClose: ImageView
    private val mDataList: MutableList<TradeAccountModel> = mutableListOf()
    private val mAdapter: SelectTradeAccountAdapter = SelectTradeAccountAdapter(mDataList)
    private var mSelectIndex = 0
    private var mCallBack: ((selectIndex: Int) -> Unit)? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        val contentView =
            LayoutInflater.from(mContext).inflate(R.layout.layout_select_trade_account_dialog, null)
        initView(contentView)
        setListener()
        setView()
        setCancelable(true)
        setCanceledOnTouchOutside(true)
        setContentView(contentView)
    }

    private fun initView(contentView: View) {
        mIvDialogClose = contentView.findViewById(R.id.iv_dialog_close)
        mLlDialogContent = contentView.findViewById(R.id.ll_dialog_content)
        mRvAccountList = contentView.findViewById(R.id.rv_account_list)
        mTvSelectAccount = contentView.findViewById(R.id.tv_select_account)
        mRvAccountList.layoutManager =
            LinearLayoutManager(mContext, LinearLayoutManager.VERTICAL, false)
        mRvAccountList.adapter = mAdapter
        mRvAccountList.isNestedScrollingEnabled = false
    }

    private fun setListener() {
        mIvDialogClose.setOnClickListener {
            dismiss()
        }
        mAdapter.setOnItemClickListener { _, _, position ->
            if (mAdapter.getSelectIndex() != position) {
                mAdapter.setSelectIndex(position)
                mCallBack?.invoke(position)
            }
            mLlDialogContent.postDelayed({
                dismiss()
            }, 100)
        }
    }

    private fun setView() {
        mTvSelectAccount.text = DynamicResourceManager.getString(mContext, string.select_account)
        mAdapter.setSelectIndex(mSelectIndex)
        mAdapter.notifyDataSetChanged()
    }

    fun setData(list: List<TradeAccountModel>) {
        mDataList.clear()
        mDataList.addAll(list)
    }

    fun setCallBack(callBack: (selectIndex: Int) -> Unit) {
        mCallBack = callBack
    }

    fun setSelectIndex(index: Int) {
        mSelectIndex = index
    }

    override fun onStart() {
        super.onStart()
        window?.decorView?.setPadding(0, 0, 0, 0)
        window?.attributes?.gravity = Gravity.BOTTOM
        window?.setBackgroundDrawable(ColorDrawable(Color.TRANSPARENT))
        window?.setLayout(
            WindowManager.LayoutParams.MATCH_PARENT, getWindowHeight()
        )
        window?.attributes?.windowAnimations = com.unionftech.common.R.style.BottomDialogAnimation
        window?.also {
            Utils.setDialogNavigationBarTransparent(it)
        }
        setView()
    }

    /**
     * 计算高度(初始化可以设置默认高度)
     *
     * @return
     */
    private fun getWindowHeight(): Int {
        val screenHeight = mContext.resources.displayMetrics.heightPixels
        val statusBarHeight = StatusBarUtil.getStatusBarHeight(mContext)
        val dp44 = mContext.resources.getDimension(com.unionftech.common.R.dimen.dp_44)
        val dp16 = mContext.resources.getDimension(com.unionftech.common.R.dimen.dp_16)
        val maxHeight = screenHeight - statusBarHeight - dp44
        LogUtils.d("SelectTradeAccountDialog", "maxHeight--->$maxHeight")
        mRvAccountList.measure(0, 0)
        var resultHeight = mRvAccountList.measuredHeight + dp44 + dp16
        if (resultHeight > maxHeight) {
            resultHeight = maxHeight
            mRvAccountList.isNestedScrollingEnabled = true
        }
        LogUtils.d("SelectTradeAccountDialog", "getWindowHeight-->$resultHeight")
        return resultHeight.toInt()
    }
}